package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.aftersales.bean.QueryShSpaceParts;
import com.jy.adscm.operation.aftersales.bean.ShSpaceParts;
import com.jy.adscm.operation.aftersales.mapper.mysql.ShSpacePartsMapper;
import com.jy.adscm.operation.aftersales.service.ShSpacePartsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.bean.JqbzPartsassembly;
import com.jy.adscm.operation.mscyb.bean.JqbzSettings;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzPartsassemblyMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzSettingsMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-19
 */
@Service
public class ShSpacePartsServiceImpl extends ServiceImpl<ShSpacePartsMapper, ShSpaceParts> implements ShSpacePartsService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
     @Autowired
     JqbzPartsassemblyMapper jqbzPartsassemblyMapper;
    @Override
    public boolean deleteShSpaceParts(Integer id) {
        ShSpaceParts shSpaceParts = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(shSpaceParts.getPid(),null)){
            JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyMapper.selectById(shSpaceParts.getPid());
            jqbzPartsassembly.setIsoutstorage(0);
            jqbzPartsassemblyMapper.updateById(jqbzPartsassembly);
        }
        deleteById(id);
        return false;
    }

    @Override
    public Page<ShSpaceParts> selectShSpaceParts(Page<ShSpaceParts> page, QueryShSpaceParts queryShSpaceParts) {
        Wrapper<ShSpaceParts> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryShSpaceParts.getName(),null)){
            wrapper.eq("parts_name",queryShSpaceParts.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryShSpaceParts.getCode(),null)){
            wrapper.like("parts_code",queryShSpaceParts.getCode());
        }
        wrapper.eq("state",queryShSpaceParts.getState());
        page= super.selectPage(page,wrapper);
        List<ShSpaceParts> shSpaceParts = page.getRecords();
        for (ShSpaceParts shSpacePart : shSpaceParts) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shSpacePart.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(shSpacePart.getCreator());
                shSpacePart.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shSpacePart.getPartsName(),null)){
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(shSpacePart.getPartsName());
                shSpacePart.setPartsNameName(jqbzSettings);
            }
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public boolean insertShSpaceParts(ShSpaceParts shSpaceParts) {
        boolean insert = insert(shSpaceParts);
        //生产部装出库
        if (AdscmBaseUtils.getInstance().isNotEmpty(shSpaceParts.getPid(),null)){
            JqbzPartsassembly jqbzPartsassembly = jqbzPartsassemblyMapper.selectById(shSpaceParts.getPid());
            jqbzPartsassembly.setIsoutstorage(1);
            jqbzPartsassemblyMapper.updateById(jqbzPartsassembly);
        }
        return insert;
    }
    @Override
    public List<JqbzPartsassembly> selectParts(String code) {
        Wrapper<JqbzPartsassembly> wrapper=new EntityWrapper<>();
        wrapper.eq("TASKSTATE","1");
        wrapper.eq("TASKFINISHSTATE","1");
        wrapper.eq("ISOUTSTORAGE",0);
        if (AdscmBaseUtils.getInstance().isNotEmpty(code,null)){
            wrapper.like("TASKCODE",code);
        }
        List<JqbzPartsassembly> jqbzPartsassemblies = jqbzPartsassemblyMapper.selectList(wrapper);
        for (JqbzPartsassembly jqbzPartsassembly : jqbzPartsassemblies) {
            JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(jqbzPartsassembly.getResourcename());
            jqbzPartsassembly.setResourcenameName(jqbzSettings.getName());
        }
        return jqbzPartsassemblies;
    }

    @Override
    public List<ShSpaceParts> selectSpace() {
        Wrapper<ShSpaceParts> wrapper=new EntityWrapper<>();
        wrapper.eq("state",0);
        List<ShSpaceParts> shSpaceParts = super.selectList(wrapper);
        for (ShSpaceParts shSpacePart : shSpaceParts) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shSpacePart.getPartsName(),null)){
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(shSpacePart.getPartsName());
                shSpacePart.setPartsNameName(jqbzSettings);
            }
        }
        return shSpaceParts;
    }
}
